﻿using BusinessEntity;
using BusinessRule;
using ce.autofac.extension;
using learun.util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace Productive.Assets
{

    public enum OperationType
    {
        /// <summary>
        /// 其他
        /// </summary>
        [Description("其他")]
        Other = 0,
        /// <summary>
        /// 登陆
        /// </summary>
        [Description("登录")]
        Login = 1,
        /// <summary>
        /// 登陆
        /// </summary>
        [Description("退出")]
        Exit = 2,
        /// <summary>
        /// 访问
        /// </summary>
        [Description("访问")]
        Visit = 3,
        /// <summary>
        /// 离开
        /// </summary>
        [Description("离开")]
        Leave = 4,
        /// <summary>
        /// 新增
        /// </summary>
        [Description("新增")]
        Create = 5,
        /// <summary>
        /// 删除
        /// </summary>
        [Description("删除")]
        Delete = 6,
        /// <summary>
        /// 修改
        /// </summary>
        [Description("修改")]
        Update = 7,
        /// <summary>
        /// 提交
        /// </summary>
        [Description("提交")]
        Submit = 8,
        /// <summary>
        /// 异常
        /// </summary>
        [Description("异常")]
        Exception = 9,
        /// <summary>
        /// 异常
        /// </summary>
        [Description("API登录")]
        ApiLogin = 10,
  
        /// <summary>
        /// 审批
        /// </summary>
        [Description("审批")]
        Audit = 11,

        [Description("支付")]
        AuditPay = 12,
    }

    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionFilter : IExceptionFilter
    {

        /// <summary>
        /// 发生异常时进入
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            if (context.ExceptionHandled == false)
            {
                context.Result = new ContentResult
                {
                    Content = new ResParameter { code = ResponseCode.exception, message = context.Exception.Message, data = "" }.ToJson(),//这里是把异常抛出。也可以不抛出。
                    StatusCode = StatusCodes.Status200OK,
                    ContentType = "text/html;charset=utf-8"
                };
            }
            context.ExceptionHandled = true;


            //var logIBLL = IocManager.Instance.GetService<LogIBLL>();
            LogEntity logEntity = new LogEntity();
            logEntity.F_CategoryId = 4;
            logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString();
            logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception);
            logEntity.F_OperateAccount = ContextHelper.GetItem("account") as string;
            logEntity.F_OperateUserId = ContextHelper.GetItem("userId") as string;
            logEntity.F_Module = context.HttpContext.Request.Path;
            logEntity.F_IPAddress = context.HttpContext.Connection.RemoteIpAddress.ToString();
            logEntity.F_ExecuteResult = -1;
            logEntity.F_ExecuteResultJson = LogRule.ExceptionFormat(context.Exception);
            //logIBLL.Write(logEntity).GetAwaiter().GetResult();
            LogRule.Write(logEntity).GetAwaiter().GetResult();
        }
    }
}
